﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TexasHoldem
{
    public class HandParser
    {
        Dictionary<char, CardFace> _faceMap = new Dictionary<char, CardFace>{ 
            {'2', CardFace.Two}, {'3', CardFace.Three}, {'4', CardFace.Four}, {'5', CardFace.Five},
            {'6', CardFace.Six}, {'7', CardFace.Seven}, {'8', CardFace.Eight}, {'9', CardFace.Nine},
            {'T', CardFace.Ten}, {'J', CardFace.Jack}, {'Q', CardFace.Queen}, {'K', CardFace.King},
            {'A', CardFace.Ace}};

        Dictionary<char, CardSuit> _suitMap = new Dictionary<char, CardSuit> {
            {'h', CardSuit.Hearts},{'c', CardSuit.Clubs}, {'d', CardSuit.Diamonds}, {'s', CardSuit.Spades}
        };

        public Hand ParseHand(string inlineHand)
        {
            var parsedCards = inlineHand.Split(' ').Select(inlineCard => ParseCard(inlineCard));
            return new Hand(parsedCards);
        }

        public string ToString(Hand hand)
        {
            return string.Join(" ", hand.Cards.Select(c => ToString(c)));
        }

        private string ToString(Card card){
            var faceSymbol = _faceMap.Keys.Single(k => _faceMap[k] == card.Face);
            var suitSymbol = _suitMap.Keys.Single(k => _suitMap[k] == card.Suit);
            return faceSymbol + "" +  suitSymbol;
        }

        private Card ParseCard(string inlineCard)
        {
            return new Card(ParseFace(inlineCard[0]), ParseSuit(inlineCard[1]));
        }

        private CardFace ParseFace(char faceSymbol)
        {
            return _faceMap[faceSymbol];
        }

        private CardSuit ParseSuit(char suitSymbol)
        {
            return _suitMap[suitSymbol];
        }
    }
}
